﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using ScriptPacker.Core;
using ScriptPacker.Core.Utils;

namespace ScriptPacker.UI
{
	public partial class fmHome : Form
	{
		public fmHome()
		{
			InitializeComponent();
			JsFileList = new List<string>();
			_JsListItems = new List<ListViewItem>();
			LoadEncodingData();
			this.sfdSaveFile.FileName = DateTime.Now.Ticks.ToString();
			loadImgList();

			//resize eventhandler
			curWinSize =Size;
			Resize+=onWinResize;
		}

		private void btnAddFiles_Click(object sender, EventArgs e)
		{
			var defaultFolder = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
			ofdJSOpener.InitialDirectory = defaultFolder;

			var retVal = ofdJSOpener.ShowDialog();
			if (retVal == DialogResult.OK) {
				List<string> jsfiles = resolveJSFiles(ofdJSOpener.FileNames);
				JsFileList.AddRange(jsfiles);
				JsFileList=JsFileList.Distinct().ToList();
				LoadJSFiles();
			}
		}

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main()
		{
			Application.Run(new fmHome());
		}

		private void btnMoveUp_Click(object sender, EventArgs e)
		{
			this.lvFileList.MoveListViewItem(true, x => {
				JsFileList.Clear();
				x.Items.Cast<ListViewItem>().ToList().ForEach(y=> {
					JsFileList.Add(y.Name);
				});
			});
		}

		private void btnMoveDown_Click(object sender, EventArgs e)
		{
			this.lvFileList.MoveListViewItem(false, x => {
				JsFileList.Clear();
				x.Items.Cast<ListViewItem>().ToList().ForEach(y =>
				{
					JsFileList.Add(y.Name);
				});
			});
		}

		private void btnDelete_Click(object sender, EventArgs e)
		{
			var items = lvFileList.SelectedItems;
			items.Cast<ListViewItem>().ToList().ForEach(x => {
				if (JsFileList.Contains(x.Name)) {
					JsFileList.Remove(x.Name);
				}
			});
			LoadJSFiles();
		}

		private void btnSort_Click(object sender, EventArgs e)
		{
			JsFileList.Sort((x, y) =>
			{
				return x.CompareTo(y);
			});
			LoadJSFiles();
		}

		private void cbxEncodingList_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.cbFastDecode.Enabled = ((ECMAScriptPacker.PackerEncoding)this.cbxEncodingList.SelectedItem != ECMAScriptPacker.PackerEncoding.None);
		}

		private void btnPackIt_Click(object sender, EventArgs e)
		{
			if (JsFileList.Count == 0)
			{
				MessageBox.Show("No js files are selected!");
				return;
			}
			try
			{
				var data = string.Empty;
				//Just remove white space and comments
				if (this.cbWhiteSpaceOnly.Checked)
				{
					data = GetStrippedFileData(true);
				}
				else
				{
					//Packer proccess:
					data = GetPackedJSFileData();
				};
				if (string.IsNullOrEmpty(data.Trim())) {
					MessageBox.Show("Js Data is empty!");
					return;
				}
				saveData(data);

			}
			catch (Exception ex) {
				MessageBox.Show(ex.Message);
			}
		}

		private void saveData(string data)
		{
			DialogResult r = this.sfdSaveFile.ShowDialog(this);
			if (r == DialogResult.OK)
			{
				Stream s = sfdSaveFile.OpenFile();
				using (TextWriter rd = new StreamWriter(s))
				{
					rd.Write(data);
					rd.Close();
				}
				s.Close();

				//save the js config file
				var cfgfile = String.Format("{0}.jscfg", this.sfdSaveFile.FileName);

				XmlUtil.ToXMLDoc<List<string>>(JsFileList).Save(cfgfile);
			}
		}

		private void cbWhiteSpaceOnly_CheckedChanged(object sender, EventArgs e)
		{
			if (this.cbWhiteSpaceOnly.Checked)
			{
				this.cbSpecialChar.Enabled = false;
				this.cbFastDecode.Enabled = false;
				this.cbxEncodingList.Enabled = false;
			}
			else {
				this.cbSpecialChar.Enabled = true;
				this.cbFastDecode.Enabled = true;
				this.cbxEncodingList.Enabled = true;
			}
		}
	}
}
